package hc.bre.data.setting

import hc.bre.data.DataBuilder

@groovy.transform.CompileStatic
class SettingBuilder extends DataBuilder<SettingItem,SettingDefinition>{
	@Override
	public void buildClassMember() {
		ruleDef.list.each { SettingItem item ->
			if(item.type=='number') {
				apdNl("public static final BigDecimal ${item.code}=${referNumber(item.value)};");
			}
			else if(item.type=='text') {
				apdNl("public static final String ${item.code}=${referText(item.value)};");
			}
			else if(item.type == 'object') {
				String clazz = item.value?:'java.util.HashMap';
				clazz = className(Class.forName(clazz));
				apdNl("public static final ${clazz} ${item.code} = new ${clazz}();")
				//properties assignment
				if(item.list) {
					apdNl("static {");
					nested{
						buildObjectItemAssignment(item);
					}
					apdNl("};");
				};
			}
			else throw new Exception("wrong setting item of ${item}");
		};
	}

	void buildObjectItemAssignment(SettingItem item) {
		item.list?.each {SettingItem prop ->
			if(prop.type=='number') {
				apdNl("${item.code}.${prop.code} = ${referNumber(prop.value)};")
			}
			else if(prop.type=='text') {
				apdNl("${item.code}.${prop.code} = ${referText(prop.value)};")
			}
			else if(prop.type == 'object') {
				apdNl("${item.code}.${prop.code} =");
				apdNl("{");
				nested{
					String clazz = prop.value?:'java.util.HashMap';
					clazz = className(Class.forName(clazz));
					apdNl("${clazz} ${prop.code} = new ${clazz}();")
					buildObjectItemAssignment(prop);
					apdNl("return ${prop.code};")
				};
				apdNl("}.call();")
			}
			else throw new Exception("wrong setting item of ${item}");
		}
	};
}
